package com.sita.redsun.other;

import com.sita.redsun.mq.MQReciveListener;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Session;

/**
 * Created by zhebinwang on 2017/3/29.
 *
 * 此类实际无用，是另外一种方式 所以没有删除
 */
public class MQReciver {


    private MQReciveListener mqReciveListener;

    //连接工厂
    private static ConnectionFactory connectionFactory;
    //连接
    private static Connection connection = null;
    //会话 接受或者发送消息的线程

    //消息的目的地
    private static Destination destination;

    private static  Session session = null;
    public void init() throws  Exception{


        mqReciveListener=new MQReciveListener();
        connectionFactory = new ActiveMQConnectionFactory
                ("dspt", "dspt", "tcp://127.0.0.1:61616");


        DefaultMessageListenerContainer dmc = new DefaultMessageListenerContainer();
        dmc.setPubSubDomain(true);
        //    获取连接
        connection = connectionFactory.createConnection();
        //    启动连接
        connection.start();
        //    创建session
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

        //    创建一个名称为fire.queue的消息队列
        destination = session.createQueue("fire.queue");
        dmc.setDestination(destination);
        dmc.setConnectionFactory(connectionFactory);
        dmc.setPubSubNoLocal(true);
        dmc.setMessageListener(mqReciveListener);
        dmc.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
        dmc.initialize();
        dmc.start();

    }


}
